package com.leo.service.impl;

import com.leo.model.UserInfo;
import com.leo.service.UserInfoService;
import com.leo.util.DBUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserInfoServiceImpl
 * @Description: 简单的增删改查
 * @Author: leo825
 * @Date: 2020-01-02 11:24
 * @Version: 1.0
 */
public class UserInfoServiceImpl implements UserInfoService {

    @Override
    public void insertUserInfo(UserInfo userInfo) {
        DBUtil.getJdbcTemple().execute("INSERT INTO USER_INFO(NAME,GENDER,AGE,REMARKS) VALUES('" + userInfo.getName() + "','" + userInfo.getGender() + "','" + userInfo.getAge() + "','" + userInfo.getRemarks() + "')");
    }

    @Override
    public void deleteUserInfo(Integer id) {
        DBUtil.getJdbcTemple().execute("DELETE FROM USER_INFO WHERE ID = " + id);
    }

    @Override
    public void updateUserInfo(Integer id, UserInfo newUserInfo) {
        DBUtil.getJdbcTemple().update("UPDATE USER_INFO SET NAME=?, GENDER=?, AGE=? ,REMARKS=? WHERE ID=?", new Object[]{
                newUserInfo.getName(),
                newUserInfo.getGender(),
                newUserInfo.getAge(),
                newUserInfo.getRemarks(),
                id
        });
    }

    @Override
    public List<UserInfo> getUserInfoList() {
        List<UserInfo> userInfos = new ArrayList<>();
        List<Map<String, Object>> results = DBUtil.getJdbcTemple().queryForList("SELECT * FROM USER_INFO");
        for (Map obj : results) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId((Integer) obj.get("ID"));
            userInfo.setName((String) obj.get("NAME"));
            userInfo.setGender("0".equals((String) obj.get("GENDER")) ? "女" : "男");
            userInfo.setAge((String) obj.get("AGE"));
            userInfo.setRemarks((String) obj.get("REMARKS"));
            userInfos.add(userInfo);
        }
        return userInfos;
    }
}
